import store from '@/store'

function getDocKeyword(editor) {
  return [...editor.getDoc().querySelectorAll('#tinymce>.page-wrapper [data-type="keyword"]')]
}

function updateKeywordList(editor) {
  const els = getDocKeyword(editor)
  const keywordList = els.map((node) => ({ ...node.dataset }))
  store.commit('xmleditor/SET_KEYWORD_LIST', keywordList)
}

export default function (editor) {
  editor.on('keywordAdd', () => {
    console.log('---keywordAdd---')
    updateKeywordList(editor)
  })

  editor.on('keywordRemove', () => {
    console.log('---keywordRemove---')
    updateKeywordList(editor)
  })

  editor.on('keywordUpdate', () => {
    console.log('---keywordUpdated---')
    updateKeywordList(editor)
  })
}
